//
//  LoginViewController.m
//  JZRJAPP
//
//  Created by mac on 16/5/25.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "LoginViewController.h"
#import "NetWorkManager.h"
#import "SettingViewController.h"
@interface LoginViewController ()

@property (nonatomic ,strong) UIView *topView;
/**
 *  手机号
 */
@property (nonatomic ,strong) UITextField * phoneText;
/**
 *  图片验证码
 */
@property (nonatomic ,strong) UITextField *verifyText;
/**
 *  短信验证码
 */
@property (nonatomic ,strong) UITextField *MessageText;


/**
 *  验证图片
 */

@property (nonatomic ,strong) UIImageView *imageView;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 隐藏上一个页面传来的tabbar
   // self.hidesBottomBarWhenPushed = YES;
    self.view.backgroundColor =[UIColor whiteColor];
    [self setUIview];
    // Do any additional setup after loading the view.
    
    
    
}

-(void)loadImage{
    NSString *url = [GetImgCode stringByAppendingString:self.phoneText.text];
    
    [NetWorkManager getUrl:url paramdic:nil success:^(id data) {
        self.imageView =[[UIImageView alloc]init];
        [self.imageView setImage:[UIImage imageNamed:data[@"Data"][@"ImgSrc"]]];
        
    } error:^(id error) {
        
        
    }];
}

-(void)setUIview{
    
    self.topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    
    self.topView.backgroundColor =[UIColor redColor];
    
    UIButton *leftBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    leftBtn.frame =CGRectMake(5, 15, 50, 50);
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    
    //[leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *nameLabel =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2 -50, 20, 100, 40)];
    nameLabel.text = @"登录";
    nameLabel.textAlignment = NSTextAlignmentCenter;
    
    UIButton *rightBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame =CGRectMake(SCREEN_WIDTH - 65, 15, 50, 50);
    
    [rightBtn setTitle: @"设置" forState:UIControlStateNormal];
    
    [rightBtn addTarget:self action:@selector(shezhi:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.topView addSubview:rightBtn];
    
    [self.topView addSubview:nameLabel];
    [self.topView addSubview:leftBtn];
    
    
    self.phoneText =[[UITextField alloc]initWithFrame:CGRectMake( 10, 80, SCREEN_WIDTH -20, 50)];
    self.phoneText.placeholder = @"请输入手机号";
    UIView *lineView =[[UIView alloc]initWithFrame:CGRectMake(10, 130, SCREEN_WIDTH - 20, 1)];
    lineView.alpha = 0.7;
    lineView.backgroundColor =[UIColor grayColor];
   
    
    self.verifyText = [[UITextField alloc]initWithFrame:CGRectMake(10, 131, SCREEN_WIDTH / 2, 50)];
    self.verifyText.placeholder=@"请输入图片验证码";
    UIView *lineView1 =[[UIView alloc]initWithFrame:CGRectMake(10, 181, SCREEN_WIDTH - 20, 1)];
    lineView1.alpha = 0.7;
    lineView1.backgroundColor =[UIColor grayColor];
    
    UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH /5 * 3.5 , 137, SCREEN_WIDTH /4 + 10, 40)];
    imageView.backgroundColor =[UIColor redColor];
    
    [imageView setImage:[UIImage imageNamed:@"achieve"]];
    
    self.MessageText = [[UITextField alloc]initWithFrame:CGRectMake(10, 181, SCREEN_WIDTH / 2, 50)];
    self.MessageText.placeholder = @"请输入短信验证码";
    

    
    UIButton *achieveBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    achieveBtn.frame = CGRectMake(SCREEN_WIDTH /5 * 3.5 , 195, SCREEN_WIDTH /4 + 10, 30);
    achieveBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [achieveBtn  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    achieveBtn.layer.cornerRadius = 5;
    achieveBtn.layer.borderColor = [UIColor redColor].CGColor;
    achieveBtn.layer.borderWidth = 1;
    [achieveBtn setTitle:@"获取短信验证码" forState:UIControlStateNormal];
    
    [achieveBtn addTarget:self action:@selector(achiveClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView *lineView2 =[[UIView alloc]initWithFrame:CGRectMake(10, 231, SCREEN_WIDTH - 20, 1)];
    lineView2.alpha = 0.7;
    lineView2.backgroundColor =[UIColor grayColor];
    
    
    //13240971668
    UIButton * loginBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(40, 271, SCREEN_WIDTH - 80, 40);
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor =[UIColor redColor];
    [loginBtn setTitle:@"验证并登录" forState:UIControlStateNormal];
    [loginBtn addTarget: self action:@selector(loginBtn:) forControlEvents:UIControlEventTouchUpInside];
    loginBtn.layer.cornerRadius = 8;
    
    
    
    [self.view addSubview:loginBtn];
    [self.view addSubview:achieveBtn];
    [self.view addSubview:lineView2];
    [self.view addSubview:self.MessageText];
    [self.view addSubview:imageView];
    [self.view addSubview:lineView];
    [self.view addSubview:lineView1];
    [self .view addSubview:self.phoneText];
    [self.view addSubview:self.verifyText];
    [self.view addSubview:self.topView];
    
    NSString *url = @"http://abiao123.xicp.net/LHWeb//Api/Login?Tel=%@&smsCode=%@";
    NSString *url1 = [[NSString alloc] initWithFormat:url, @"18315915920", @"1234"];
    NSLog(@"pinjie%@",url1);
}

-(void)loginBtn:(UIButton *)sender{
    NSLog(@"登陆");
    NSString *url = Login;
     NSString *url1 = [[NSString alloc] initWithFormat:url, self.phoneText.text, self.MessageText.text];
    NSLog(@"%@",url1);
     [NetWorkManager getUrl:url1 paramdic:nil success:^(id data) {
         
         NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
         [accountDefaults setObject:data[@"Data"][@"Token"]forKey:@"Token"];
         [accountDefaults setBool:data[@"State"] forKey:@"State"];
         [accountDefaults setObject:self.phoneText.text forKey:@"phone"];
         
         NSLog(@"%@",data[@"State"]);
         NSLog(@"%@",data[@"Data"][@"Token"]);
         
         
     } error:^(id error) {
         
         
     }];
}

-(void)achiveClick:(UIButton *)sender{
    //[self loadImage];
    NSString *url = [GetSMSCode stringByAppendingString:self.phoneText.text];
    NSLog(@"%@",url);
    [NetWorkManager  getUrl:url paramdic:nil success:^(id data) {
        
        NSLog(@"%@",data[@"Data"]);
        
    } error:^(id error) {
        
        
    }];
    NSLog(@"获取验证码");
}
-(void)backClick:(UIButton *)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)shezhi:(UIButton *)sender{
    SettingViewController *setVC =[[SettingViewController alloc]init];
    [self presentViewController:setVC animated:YES completion:nil];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
